﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.DomainLayer;

using System.Collections.ObjectModel;

namespace HA.ModFlow.Controls
{
   public class PCRiverCollecion:PCBase
    {

       public PCRiverCollecion(RiverCollection rc)
       {

           this._riverCollecion = rc;

           this._periodStep = new List<int>();

           for (int i = 0; i < rc.NPeriods; i++)
           {

               this._periodStep.Add(i+1);
               
           }


           this._lstUsePrecious = new List<bool>();

           this._lstUsePrecious.AddRange(rc.IsUsePrecious);
          



           this._rivers = new ObservableCollection<PCRiver>();

           foreach (River item in rc.InnerList)
           {
               PCRiver pc = new PCRiver((River)item.Clone());

               this._rivers.Add(pc);
               
           }



 
       }


       RiverCollection _riverCollecion;




       bool _isShowId = true;

       public bool IsShowId
       {
           get { return _isShowId; }
           set { _isShowId = value; }
       }




       List<bool> _lstUsePrecious;

       public bool UsePreciousEnabled
       {
           get
           {
               if (this._currentPeriod == 0)
               {
                   return false;
               }
               else
               {
                   return true;
               }
           }
 
       }

        public bool UsePrecious
        {
            get {
                if (_lstUsePrecious.Count == 0)
                    return false;
                return this._lstUsePrecious[this._currentPeriod]; 
            
            
            }
            set {



                this._lstUsePrecious[this._currentPeriod] = value;
                this._riverCollecion.IsUsePrecious[this._currentPeriod] = value;
             
                this.OnPropertyChanged("UsePrecious");
            
            }
        }


       //具体的index值

        int _currentPeriod=0;

        public int CurrentPeriod
        {
            get { return _currentPeriod; }
            set {
                
                
                _currentPeriod = value;

                this.UsePrecious = this._riverCollecion.IsUsePrecious[_currentPeriod];

                foreach (PCRiver item in this._rivers)
                {
                    item.CurrentIndex = this._currentPeriod;
                    
                }

                this.OnPropertyChanged("UsePreciousEnabled");

                this.OnPropertyChanged("UsePrecious");
            
            
            }
        }





        List<int> _periodStep;

        public List<int> PeriodStep
        {
            get { return _periodStep; }
           
        }



        ObservableCollection<PCRiver> _rivers;

        public ObservableCollection<PCRiver> Rivers
        {
            get { return _rivers; }
            set { _rivers = value; }
        }

        //List<PCRiver> _rivers;



        public void CreateNew()
        {
            River river = new River(1, 1, 1, this._riverCollecion.Nrows, this._riverCollecion.Ncols, this._riverCollecion.Nlayers, this._periodStep.Count);

            PCRiver pcNew = new PCRiver(river);

            this.Rivers.Add(pcNew);
        }





        public void ConfirmData()
        {

            List<River> rivers = new List<River>();

            foreach (PCRiver item in this._rivers)
            {
                rivers.Add(item.River);
                
            }

            this._riverCollecion.ResetRivers(rivers);

            this._riverCollecion.ResetIsUsePrecious(this._lstUsePrecious);




 
        }
        /// <summary>
        /// 检查层行列的正确定性
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="layer"></param>
        /// <param name="index"></param>
        /// <param name="isIndexId"></param>
        /// <returns></returns>
        public bool CheckInputIJKorIndex(int row, int col, int layer, int index, bool isIndexId)
        {
            foreach (PCRiver item in this._rivers)
            {
                if (!isIndexId)
                {
                    if (item.I <= 0 || item.I > row)//行
                    {
                        System.Windows.MessageBox.Show("不存在第 " + item.I + " 行，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                        return false;
                    }
                    if (item.J <= 0 || item.J > col)
                    {
                        System.Windows.MessageBox.Show("不存在第 " + item.J + " 列，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                        return false;
                    }
                    if (item.K <= 0 || item.K > layer)
                    {
                        System.Windows.MessageBox.Show("不存在第 " + item.K + " 层，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                        return false;
                    }
                }
                else
                {
                    if (item.Index <= 0 || item.Index > index)
                    {
                        System.Windows.MessageBox.Show("不存在 " + item.Index + " 单元格ID，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                        return false;
                    }

                }
            }

            return true;
        }


    }

   public class PCRiver:PCBase
   {


       public PCRiver(River river)
       {
           this._river = river;
 
       }

       River _river;

       public River River
       {
           get { return _river; }
         
       }



       int _currentIndex=0;

       public int CurrentIndex
       {
           get { return _currentIndex; }
           set { 
               
               
               _currentIndex = value;


           this.OnPropertyChanged("Stage");
           this.OnPropertyChanged("Conductance");
           this.OnPropertyChanged("Elevation");


           
           }
       }




       public int Index
       {
           get
           {
               return this._river.Index;
              

           }
           set
           {


               this._river.Index = value;

               this.OnPropertyChanged("I");
               this.OnPropertyChanged("J");
               this.OnPropertyChanged("K");

           }
       }

     

       public int I
       {
           get { return this._river.I; }
           set
           {
               this._river.I = value;

               this.OnPropertyChanged("Index");


           }

       }
      

       public int J
       {
           get { return this._river.J; }
           set
           {
               this._river.J = value;

               this.OnPropertyChanged("Index");
           }
       }
      

       public int K
       {
           get { return  this._river.K; }
           set
           {
               this._river.K = value;


               this.OnPropertyChanged("Index");

           }
       }


       public double Stage
       {
           get { return this._river.Stage[this._currentIndex]; }

           set { this._river.Stage[this._currentIndex] = value; }
       }


       public double Conductance
       {
           get { return this._river.Conductance[this._currentIndex]; }

           set { this._river.Conductance[this._currentIndex] = value; }
 
       }

       public double Elevation
       {

           get { return this._river.Elevation[this._currentIndex]; }

           set { this._river.Elevation[this._currentIndex] = value; }
 

       }
 
   }
}
